<?php
namespace App\Http\Controllers\Admin\FBA\InboundShipment;

//use App\Common\EC\Common;
use App\Http\Controllers\Controller;
use App\Model\FBA\InboundShipment;
use App\Model\FBA\InboundShipmentItem;
use Illuminate\Http\Request;

class Detail extends Controller
{

//    use Common;


    /**
     * 查找 Shipment
     * @param string $id
     */
    static function find ($id)
    {
        $model = InboundShipment::find($id);
        return $model ?: self::debug(self::resFailOfNoData(InboundShipment::NAME, $id));
    }



    /**
     * 详情 (Basic Info)
     * @param $ShipmentId string
     */
    public function main(Request $req) {
        $ShipmentId = $req->ShipmentId;

//        Model::ec($req->get('ec', 'us'));
        $qry = InboundShipment::query();
        if (!$qry->find($ShipmentId)) return $this->resFail('ShipmentId 有误！');

        $shipment= $qry->select('*')
            ->with('item')
//            ->whereEcId()
            ->find($ShipmentId);

        appendProductById($shipment->item, ['mfn','name','cover']);
        $shipment->translateProperties();
        $shipment->_from = @$shipment->warehouseFrom->name;
        $shipment->_to = @$shipment->warehouseTo->name;
        $shipment->ec_no = self::ecNo($shipment->ec_id);
        unset($shipment->warehouseFrom, $shipment->warehouseTo);

        foreach($shipment->item as $v) {
            $v->sell_pack = productSellpack($v->product_id);
        }

        return $this->resSuccess('获取成功', $shipment);
    }


    /**
     * 编辑Shipment
     * @param $ShipmentId string
     */
    public function update(Request $req) {
        $shipment = InboundShipment::find($req->ShipmentId);

        if (!$shipment) return $this->resFail('ShipmentId 有误！');
        if (!$shipment->canEdit()) {
            return $this->resFail($shipment->debug());
        }
        if (!$req->ShipmentName) {
            return $this->resFail('Name 必须');
        }

        if ($shipment->step > 2) {
            return $this->resFail('该功能不可用');

        } else if ($shipment->step == 1){

            if (!$req->LabelPrepType || !isset($req->AreCasesRequired) ) {
                return $this->resFail('LabelType 和 原厂包装 必须');
            }
            $shipment->ShipmentName = $req->ShipmentName;
            $shipment->LabelPrepType = $req->LabelPrepType;
            $shipment->AreCasesRequired = $req->AreCasesRequired;

        } else if ($shipment->step == 2){
            $shipment->ShipmentName = $req->ShipmentName;
        }

        if($shipment->save()) {
            return $this->resSuccess('操作成功');
        }

        return $this->resFail('操作失败');

    }

    /**
     * 删除Shipment
     * @param $ShipmentId string
     */
    public function delete(Request $req) {
        $shipment = InboundShipment::find($req->ShipmentId);

        if (!$shipment) return $this->resFail('ShipmentId 有误！');
        if (!$shipment->canEdit()) {
            return $this->resFail($shipment->debug());
        }
        /*if ($shipment->step > 2) {
            return $this->resFail('该功能不可用');
        }*/

        if($shipment->delete()) {
            return $this->resSuccess('操作成功');
        }

        return $this->resFail('操作失败');

    }

    /**
     * 编辑 Address From
     * @param $ShipmentId string
     */
    public function updateAddr(Request $req)
    {
        return 1;
        $shipment = InboundShipment::find($req->ShipmentId);

        if (!$shipment) return $this->resFail('ShipmentId 有误！');
        if ($shipment->step > 1) {
            return $this->resFail('该功能不可用');
        }


    }

    /**
     * 编辑 一个Shipment Item
     * @param $ShipmentId string
     * @param $product_id int
     * @param $qty int
     */
    public function updateItem(Request $req)
    {
        if (!$req->ShipmentId || !$req->product_id) {
            return $this->resFail('参数错误');
        }

        if (intval($req->qty)==0) {
            return $this->resFail('数量必须是正整数');
        }

        $shipment = InboundShipment::find($req->ShipmentId);


        if (!$shipment) return $this->resFail('ShipmentId 有误！');
        /*if ($shipment->step > 1) {
            return $this->resFail('该功能不可用');
        }*/
        if (!$shipment->canEdit(true)) {
            return $this->resFail($shipment->debug());
        }

        $item = InboundShipmentItem::where('ShipmentId' , $req->ShipmentId)
            ->where( 'product_id' ,$req->product_id)
            ->first();

        $item->QuantityShipped = $req->qty;
        $item->QuantityInCase = $req->qty;
        if ($item->debug()) {
            return $this->resFail($item->debug());
        }

        if($item->save() ) {
            return $this->resSuccess('操作成功');
        }

        return $this->resFail('操作失败');
    }

    /**
     * 删除 一个Shipment Item
     * @param $ShipmentId string
     * @param $product_id int
     */
    public function deleteItem(Request $req)
    {
        if (!$req->ShipmentId || !$req->product_id) {
            return $this->resFail('参数错误');
        }

        $shipment = InboundShipment::find($req->ShipmentId);
        if (!$shipment) return $this->resFail('ShipmentId 有误！');
        if (!$shipment->canEdit(true)) {
            return $this->resFail($shipment->debug());
        }

        $res = InboundShipmentItem::where('ShipmentId' , $req->ShipmentId)
            ->where('product_id' ,$req->product_id)
            ->delete();

        if($res) {
            return $this->resSuccess('操作成功');
        }

        return $this->resFail('操作失败');
    }



    /**
     * 提交确认 Shipment
     * @param string $ShipmentId
     */
    public function confirm(Request $req) {
        $shipment = self::find($req->post('ShipmentId'));

        if (!$shipment) return self::debug();

        if (!$shipment->canConfirm()) {
            return $this->resFail($shipment->debug());
        }

        switch ($shipment->step) {
            // 本地->Plan
            case 1:
                $shipment->status = 1;
                $job = new \App\Jobs\FBA\InboundShipment\UploadPlan($shipment, null);
                break;

            // Plan->Shipment
            case 2:
                $shipment->status = 2;
                $job = new \App\Jobs\FBA\InboundShipment\UploadShipment($shipment, null);
                break;

            case 3:
                // Shipment->发货或删除
                $shipment->status = 4;
                $job = new \App\Jobs\FBA\InboundShipment\UpdateShipment($shipment, null, 1);
        }

        $shipment->save();
        @$job && dispatch($job);

        return $this->resSuccess('提交成功。', [
            'status' => $shipment->status,
            '_status' => $shipment->translateProperty('status'),
        ]);

    }



}
